
package combat.dudes.client.gui.threads.map;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import combat.dudes.DudesFunctions;
import combat.dudes.client.ClientApp;
import combat.dudes.client.gui.threads.map.main_old.GUIMainMap;
import combat.dudes.client.gui.threads.shell.GUIShell;
import combat.dudes.protocol.data.DoublePoint;


/**
 * @author Laurens Andriessen
 * @version 1.0
 */
public class GUIMapLand extends JPanel {
	private static final long serialVersionUID = 1L;

	private static BufferedImage image = null;
	public static synchronized BufferedImage getImage() {
		return image;
	}

	public GUIMapLand() {}

	@Override
	public void paintComponent(Graphics g) {
		Graphics2D _g2 = (Graphics2D) g;
		if(image == null) {
			if(ClientApp.SharedVars.getScenarioLand() != null) {
				int mapW = GUIShell.windowWorldView.myBodyPanel.getWidth();
				int mapH = GUIShell.windowWorldView.myBodyPanel.getHeight();
				setBounds(0, 0, mapW, mapH);

				// http://www.java.net/node/683925
				image = new BufferedImage(mapW + 1, mapH + 1, BufferedImage.TYPE_INT_ARGB);
				Graphics2D g2 = (Graphics2D) image.getGraphics();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Set AA on for shapes
				g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); // Set AA off for text
				g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED); // Set render focus on speed
				// Transparent background
				g2.setBackground(new Color(0, 0, 0, 0));
				g2.clearRect(0, 0, image.getWidth(), image.getHeight());

				/* // Sea
				 * g2.setColor(ClientGUITheme.guiMainMapSea);
				 * g2.fillRect(0, 0, mapW, mapH);
				 * 
				 * // Land, painted *over* sea
				 * g2.setColor(ClientGUITheme.guiMainMapLand);
				 * if(ClientApp.SharedVars.getScenarioLand().npoints > 0) g2.fillPolygon(ClientApp.SharedVars.getScenarioLand()); */

				g2.drawImage(DudesFunctions.File.getImage("land.gif"), (int) GUIMainMap.getViewCenter().getX(), (int) GUIMainMap.getViewCenter().getY(), this);
				System.out.println(GUIMainMap.getViewCenter().getX());
			}
		}
		_g2.drawImage(image, null, 0, 0);
	}

	public static Boolean isOnLand(DoublePoint position) {
		Polygon land = ClientApp.SharedVars.getScenarioLand();
		if(land != null) {
			Point pos = new Point((int) position.getX(), (int) position.getY());
			return land.contains(pos);
		} else return true;
	}

}
